package com.example.freeengine.wallpaper.fragment.everydayrefresh;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.freeengine.R;
import com.example.freeengine.databinding.FragmentWallPaperEveryDayRefreshHistoryBinding;
import com.example.freeengine.wallpaper.fragment.WallPaperViewModel;
import com.example.freeengine.wallpaper.fragment.everydayrefresh.historylist.HistoryPictureListAdapter;
import com.example.freeengine.wallpaper.fragment.everydayrefresh.picturesource.BingPictureSource;
import com.example.freeengine.wallpaper.network.VolleyCallback;


/**
 * @author Terry Deng
 */
public class WallPaperEveryDayRefreshHistoryFragment extends Fragment {

    FragmentWallPaperEveryDayRefreshHistoryBinding viewBinding;
    HistoryPictureListAdapter historyPictureListAdapter;
    WallPaperViewModel wallPaperViewModel;

    public WallPaperEveryDayRefreshHistoryFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewBinding = FragmentWallPaperEveryDayRefreshHistoryBinding.inflate(inflater);
        return viewBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        wallPaperViewModel = new ViewModelProvider(requireActivity()).get(WallPaperViewModel.class);

        assert getArguments() != null;
        historyPictureListAdapter = new HistoryPictureListAdapter(wallPaperViewModel.getEverydayRefreshHistoryPictures(getArguments().getString("path")),
                requireContext(), wallPaperViewModel);
        viewBinding.recyclerViewEveryDayRefreshHistoryPictureList.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        viewBinding.recyclerViewEveryDayRefreshHistoryPictureList.setAdapter(historyPictureListAdapter);

        wallPaperViewModel.getEverydayRefreshHistoryDataChange().observe(requireActivity(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer change) {
                if (change == 1){
                    historyPictureListAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}